<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>优惠券列表</title>
    <!--{include file='comm/comm_cal.tpl'}-->
</head>
<body>

<div class="container">

 
    <div class="comm_content">
            <div  class="search-box">
                <form id="searchForm" class="form-inline" name="searchForm" onsubmit="return false;">

                    <div style="position:relative">
                        <span>
                            优惠券编号：
                            <input type="text" id="code" class="form-control input-sm"  name="code" value="" style="width:120px;"/>
                        </span>
                        <span>
                            优惠券名称：
                            <input type="text" id="couponName" class="form-control input-sm"  name="couponName" value="" style="width:130px;"/>
                        </span>
                        <span>
                            有效时间：
                            <input id="startDate" class="form-control input-sm"  name="startDate" value="" type='text' style="width:100px;" readonly="readonly" >
                            ～
                            <input id="endDate" class="form-control input-sm"  name="endDate" value="" type='text' style="width:100px;" readonly="readonly" >
                        </span>
                        <span>
                            发放类型：
                            <select class="form-control" name="couponType">
                                <option value="0">请选择</option>
                                <option value="1">编号礼品卡</option>
                                <option value="2">账户礼品券</option>
                                <option value="3">万能码</option>
                            </select>
                        </span>
                        <span>
                            <input type="button" class="btn btn-primary" value="查询" onclick="coupon.search();">&nbsp;&nbsp;
                            <input type="button" class="btn btn-warning" value="重置" onclick="coupon.clear();">
                        </span>
                    </div>
                </form>
            </div>

            <table id="flex" style="display:none"></table>

            <!--导出 开始-->
            <form id="export_from" action="/quanliadmin/app/promotion/coupon.pscript?operFlg=7" method="post" name="export_from">
                <input id="coupon_id" type="hidden" value="" name="couponID">
                <input id="totalNum" type="hidden" value="" name="totalNum">
            </form>
            <!--导出 结束-->

            <!--导入 开始-->
            <div id="import" style="display: none;">
                <form id="upload_form" name="upload_form" method="post" enctype="multipart/form-data" action="/quanliadmin/app/promotion/coupon.pscript?operFlg=6&couponID=" target="hide_frame" >
                    <span style="display: inline-block;">
                        <input size="30" type="file" id="search_file" name="search_file" >
                    </span>
                    <input type="button" name="into_sub" id="into_sub" class="btn btn-primary" value="导入">
                    <span id="importMsg" style="line-height: 30px;"></span>
                    <iframe id="hide_frame" name="hide_frame" style="display: none;"></iframe>
                </form>
            </div>
            <!--导入 结束-->


            <!--大批量导入 开始-->
            <div id="bigImport" style="display: none;">
                <form id="bigUpload_form" name="bigUpload_form" method="post" enctype="multipart/form-data" action="/quanliadmin/app/promotion/coupon.pscript?operFlg=8&couponID=" target="hide_frame" >
                    <span style="display: inline-block;">
                        <input size="30" type="file" id="bigSearch_file" name="bigSearch_file" >
                    </span>
                    <input type="button" name="bigInto_sub" id="bigInto_sub" class="btn btn-primary" value="导入">
                    <span id="bigImportMsg" style="line-height: 30px;"></span>
                    <iframe id="bigHide_frame" name="bigHide_frame" style="display: none;"></iframe>
                </form>
            </div>
            <!--大批量导入 结束-->

        </div>


</div>

<script type="text/javascript">

    //导入 上传start
    $(document).on('click','#into_sub',function(){
        var filename = $('#search_file').val();
        if(filename == ''){
            coupon.importMsg('请选择上传文件',0);
            return false;
        }

        var basename = filename.split(".");
        if(basename[basename.length-1].toLowerCase() != 'csv'){
            coupon.importMsg('文件类型错误，请选择CSV文件',0);
            return false;
        };

        $("#importMsg").css("color","black");
        $("#importMsg").text("正在导入，请稍后...");
        $("#upload_form").submit();
    });
    //导入 上传end

    //大批量导入 上传start
    $(document).on('click','#bigInto_sub',function(){
        var filename = $('#bigSearch_file').val();
        if(filename == ''){
            coupon.importMsg('请选择上传文件',0);
            return false;
        }

        var basename = filename.split(".");
        if(basename[basename.length-1].toLowerCase() != 'csv'){
            coupon.importMsg('文件类型错误，请选择CSV文件',0);
            return false;
        };

        $("#bigImportMsg").css("color","black");
        $("#bigImportMsg").text("正在导入，请稍后...");
        $("#bigUpload_form").submit();
    });
    //大批量导入 上传end

    $('#startDate').datetimepicker();
    $('#endDate').datetimepicker();

    $("#flex").flexigrid({
        url:"/invokingAjax-backend/promotion/coupon.ajax?operFlg=1",
        dataType: "json",
        colModel : [
            {display: "操作", width : 250, align: 'center'},
            {display: "使用状态", width : 70, align: 'center'},
            {display: "编号", width : 50, align: 'center',  name : "couponID", sortable : true},
            {display: "优惠券名称", width : 180 },
            {display: "开始时间", width : 140, align: 'center'},
            {display: "结束时间", width : 140, align: 'center'},
            {display: "使用限额", width : 80, align : 'center'},
            {display: "抵扣金额", width : 60, align : 'center'},
            {display: "优惠券类型", width : 100, align : 'center'},
            {display: "优惠券使用类型", width : 100},
            {display: "使用城市", width : 100},
            {display: "万能码编号",width : 100, align:'left'},
            {display: "发放数目[0表示无限制]", width : 130, align : 'right'},
            {display: "已领取数量", width : 80, align : 'right'},
            {display: "已使用数目", width : 80, align : 'right'}
        ],
        buttons : [
            {name: "添加", bclass: "add", onpress : "coupon.edit"}
        ],
        sortname: "couponID",
        sortorder: "desc",
        usepager: true,
        rp: 10,
        useRp: true,
        showTableToggleBtn: true
    });

    var coupon = {

        couponID: "",

        /**
         * 查找
         */
        search : function() {
            var dt = $("#searchForm").serializeArray();
            $("#flex").flexOptions({newp: 1, params: dt}).flexReload();
        },

        /**
         * 重置表单
         */
        clear : function() {
            document.forms['searchForm'].reset();
            this.search();
        },

        /**
         * 新增或修改
         * @param id
         */
        edit: function( couponID ) {
            if (couponID) {
                E.open("/quanliadmin/app/promotion/coupon.pscript?operFlg=2&couponID=" + couponID, "", {height : 600,width: 1024});
            } else {
                E.open("/quanliadmin/app/promotion/coupon.pscript?operFlg=2", "", {height : 600,width: 1024});
            }
        },

        //发放账户优惠券
        grant: function( couponID ) {

            E.open("/quanliadmin/app/promotion/coupon.pscript?operFlg=5&couponID=" + couponID, "", {height : 600});

        },

        //导入优惠券
        import:function( couponID ){
            $("#upload_form").attr("action","/quanliadmin/app/promotion/coupon.pscript?operFlg=6&couponID="+couponID);
            E.popup.open({
                id: 'import',
                title: '导入优惠券',
                css: 'width: 800px;'
            });
        },

        /**
         * 导入优惠券的信息
         * @param msg  文字信息
         * @param flg  0:错误 1:成功
         */
        importMsg : function (msg,flg){
            if(flg == 0){
                $("#importMsg").css("color","red");
            }else{
                $("#importMsg").css("color","green");
                //更新 input file
                var input_html = $("#search_file").clone();
                $("#search_file").remove();
                $("#searchForm").prepend(input_html);
                msg = "导入成功";
            }
            $("#importMsg").text(msg);
        },


        //大批量导入优惠券
        bigImport:function( couponID ){
            $("#bigUpload_form").attr("action","/quanliadmin/app/promotion/coupon.pscript?operFlg=8&couponID="+couponID);
            E.popup.open({
                id: 'bigImport',
                title: '导入优惠券',
                css: 'width: 800px;'
            });
        },

        /**
         * 大批量导入优惠券的信息
         * @param msg  文字信息
         * @param flg  0:错误 1:成功
         */
        bigImportMsg : function (msg,flg){
            if(flg == 0){
                $("#bigImportMsg").css("color","red");
            }else{
                $("#bigImportMsg").css("color","green");
                //更新 input file
                var input_html = $("#bigSearch_file").clone();
                $("#bigSearch_file").remove();
                $("#bigSearchForm").prepend(input_html);
                msg = "导入成功";
            }
            $("#bigImportMsg").text(msg);
        },

        //导出优惠券信息
        exportCoupon:function( couponID , totalNum ){
            $("#coupon_id").val( couponID );
            $("#totalNum").val( totalNum );
            var totalNum = totalNum;

            if( totalNum <= 5000 ){
                coupon.export();
                return false;
            }

            var page = Math.ceil( totalNum/5000 );

            this.totalPage = page;

            var html = "<table>";
            html += "<tr>";
            html += "<td width=\"100\" style='text-align: center;'><b>当前数据已超过5000条，请选择区间导出</b></td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td width=\"100\" style='text-align: center;'>5000条为一个区间，当前数据共 <b style='font-size: 14px;'>"+page+"</b> 页</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td>导出第<input type='text' id='page' value='1' >页数据</td>";
            html += "</tr>";
            html += "</tbody>";
            html += "</table>";

            E.popup.open({
                title: "选择优惠券导出区间",
                content: html,
                btnName: "导出",
                css:"width:300px;",
                btnCall: "coupon.choseExportRegion();"
            });

        },

        //选择打印区间
        choseExportRegion:function(){

            var page = $.trim( $("#page").val() );

            if( page == '' ){
                E.alert('请填写导出页数');
                return false;
            }

            if( !E.isInt( page ) || page <= 0 ){
                E.alert('请填写正确的页数');
                return false;
            }

            if( page > this.totalPage ){
                E.alert('导出页数不能大于总页数');
                return false;
            }

            document.getElementById('export_from').action = '/quanliadmin/app/promotion/coupon.pscript?operFlg=7&page='+page;
            document.getElementById('export_from').submit();

        },

        export: function() {
            document.getElementById('export_from').action = '/quanliadmin/app/promotion/coupon.pscript?operFlg=7';
            document.getElementById('export_from').submit();
        },

        status: {

            change: function( couponID, useFlg ) {
                coupon.couponID = couponID;
                if (useFlg == 0) {
                    E.confirm("您确认要启用该优惠券吗？", "coupon.status.execute");
                } else {
                    E.confirm('您确认要禁用该优惠券吗？', "coupon.status.execute");
                }
            },

            execute: function() {

                E.loadding.open("正在变更优惠券使用状态，请稍候...");
                E.ajax_get({
                    url: "/invokingAjax-backend/promotion/coupon.ajax?operFlg=3",
                    data: {
                        couponID: coupon.couponID
                    },
                    call: "coupon.status.result"
                });

            },

            result: function( o ) {

                E.loadding.close();
                if (o.code == 200) {
                    E.alert(o.message, 2, "coupon.load");
                } else {
                    E.alert(o.message);
                }

            }

        },

        create: {

            execute: function( couponID ) {

                E.loadding.open("正在生成优惠券编号，请稍候...");
                E.ajax_get({
                    url: "/invokingAjax-backend/promotion/coupon.ajax?operFlg=5",
                    data: {
                        couponID: couponID
                    },
                    call: "coupon.create.result"
                });

            },

            result: function( o ) {

                E.loadding.close();
                if (o.code == 200) {
                    E.alert(o.message, 2, "coupon.load");
                } else {
                    E.alert(o.message);
                }

            }

        },


        /**
         * 刷新列表
         */
        load : function(){
            $("#flex").flexReload();
        },

        /**
         *
         */

    };
</script>

</body>
</html>